package com.sugon.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.sugon.annotation.AutoLog;
import com.sugon.dao.SysMsgDao;
import com.sugon.entity.SysMsgEntity;
import com.sugon.entity.SysUserMsgEntity;
import com.sugon.modules.utils.StrUtil;
import com.sugon.service.SysMsgService;
import com.sugon.service.SysUserMsgService;
import com.sugon.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 系统用户Controller
 *
 * @author sunlianqiang
 * @date 2019-07-15 17:04:30
 */
@RestController
@RequestMapping("/sysMsg")
public class SysUserMsgController {

    @Autowired
    private SysUserMsgService sysUserMsgService;
    @Autowired
    private SysMsgService msgService;
    @Autowired
    private SysMsgDao msgDao;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sysuser:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<SysUserMsgEntity> sysUserList = sysUserMsgService.queryList(query);
        int total = sysUserMsgService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(sysUserList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{userId}")
    @RequiresPermissions("sysuser:info")
    @ResponseBody
    public R info(@PathVariable("userId") String userId) {
        SysUserMsgEntity sysUser = sysUserMsgService.queryObject(userId);

        return R.ok().put("sysUser", sysUser);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sysuser:save")
    @ResponseBody
    public R save(@RequestBody SysUserMsgEntity sysUser) {
        sysUserMsgService.save(sysUser);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sysuser:update")
    @ResponseBody
    public R update(@RequestBody SysUserMsgEntity sysUser) {
        sysUserMsgService.update(sysUser);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sysuser:delete")
    @ResponseBody
    public R delete(@RequestBody String[] userIds) {
        sysUserMsgService.deleteBatch(userIds);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<SysUserMsgEntity> list = sysUserMsgService.queryList(params);

        return R.ok().put("list", list);
    }


    /**
     * 系统消息提示
     */
    @RequestMapping({"/usermsg"})
    @ResponseBody
    public R usermsg(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        if (!StringUtils.isNotEmpty(userId)) {
            return R.error("用户ID错误");
        }
        return R.ok().put("USERMSG", sysUserMsgService.queryUserMsg(userId));
    }

    @RequestMapping({"/upMsgDataResource"})
    @ResponseBody
    public R upMsgDataResource(SysMsgEntity msgEntity) throws Exception {
        Integer upFlag = this.msgService.upSysMsgDataService(msgEntity);
        if (upFlag > 0) {
            return R.ok("编辑成功");
        }
        return R.error(500, "编辑失败");
    }


    @RequestMapping("/messageReaded")
    @ResponseBody
    public R messageReaded(@RequestParam("msgIdList[]") String[] msgIdList) {
        List<String> msgList = CollectionUtil.toList(msgIdList);
        if (CollectionUtil.isEmpty(msgList)) {
            return R.error("请先选中后再执行删除操作！");
        }
        String whereSql = StrUtil.generateBatchSql(msgList, "ID", 1000);
        return msgDao.batchMarkReaded(whereSql) > 0 ? R.ok() : R.error();
    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    public R batchDelete(@RequestParam("msgIdList[]") String[] msgIdList) {
        List<String> msgList = CollectionUtil.toList(msgIdList);
        if (CollectionUtil.isEmpty(msgList)) {
            return R.error("请先选中后再执行删除操作！");
        }
        String whereSql = StrUtil.generateBatchSql(msgList, "ID", 1000);
        return msgDao.batchDelete(whereSql) > 0 ? R.ok() : R.error();
    }

    @RequestMapping({"/queryMsgDataResource"})
    @ResponseBody
    public R queryMsgDataResource(HttpServletRequest request) throws Exception {
        String type = request.getParameter("type");
        List listAll = this.msgService.querySysMsgService(type, ShiroUtils.getUserId());
        return R.ok().put("list", listAll);
    }

    /**
     * 查询数量
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping({"/queryMsgNum"})
    @ResponseBody
    public R queryMsgNum(HttpServletRequest request) throws Exception {
        String type = request.getParameter("type");
        List listAll = this.msgService.querySysMsgService(type, ShiroUtils.getUserEntity().getDeptId());
        return R.ok().put("num", listAll.size());
    }

    @RequestMapping({"/queryMsgDataNum"})
    @ResponseBody
    public R queryMsgDataNum() throws Exception {
        List listAll = this.msgService.seeSysMsgService(ShiroUtils.getUserId());
        return R.ok().put("list", listAll);
    }


    @RequestMapping({"/sysMsgData"})
    @ResponseBody
    public R handleMsgData(SysMsgEntity msgEntity) throws Exception {
        /**
         * 流程id
         */
        String dataId = msgEntity.getDataId();
        /**
         * 流程类型
         */
        String dataType = msgEntity.getDataType();
        /**
         * 流程是否已经结束(已经结束的话直接传'1'过来就行，没结束的时候不要传值或者传'0'进来)
         */
        String dataFlag = msgEntity.getDataFlag();
        if (!StringUtils.isNotEmpty(dataId) || !StringUtils.isNotEmpty(dataType)) {
            return R.error(500, "新增的参数不能为空");
        }
        if ("1".equals(dataFlag)) {
            Integer flagMsg = this.msgService.delSysMsgService(dataId, dataType, null);
            if (flagMsg > 0) {
                return R.ok("删除成功");
            } else {
                return R.error(500, "删除失败");
            }
        }
        List<SysMsgEntity> msgOnly = this.msgService.listAll(dataId, dataType, msgEntity.getUserId());
        if (CollectionUtil.isEmpty(msgOnly)) {
            Integer flagMsg = this.msgService.inSysMsgService(msgEntity);
            if (flagMsg > 0) {
                return R.ok("新增成功");
            } else {
                return R.error(500, "新增失败");
            }
        } else {
            Integer flagMsg = this.msgService.upSysMsgService(msgEntity);
            if (flagMsg > 0) {
                return R.ok("修改成功");
            } else {
                return R.error(500, "修改失败");
            }
        }

    }

    @RequestMapping(value = "/queryDept", method = RequestMethod.GET)
    @ResponseBody
    public R queryDept() {
        List<Map<String, Object>> deptList = msgService.queryDept();
        return R.ok().put("deptList", deptList);
    }
}
